package edu.bit.water.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import edu.bit.water.dao.EntityEvaCtestSumDao;

@Service
public class EntityEvaCtestSumService {

	@Autowired
	private EntityEvaCtestSumDao entityEvaCtestSumDao;
	private EntityEvaCtestSumService entityEvaCtestSumService;

	/**
	 * 插入數值
	 * 
	 * @param list       真实數值
	 * @param listColumn 列名
	 * @return succss and failed message
	 */
	public Map<String, Object> insertEntityEvaCtestSum(List<Object> list, List<String> listColumn) {
		Map<String, Object> insertMap = new HashMap<>();
		Map<String, Object> modelMap = new HashMap<>();
		// 固定字段
		String strSum = "water_class,use_id,water_unit_name";
		for (String str : listColumn)
			strSum += ',' + str;
		strSum += ",label_id";// 變化字段在固定字段中間
		insertMap.put("lineColumn", strSum);
		insertMap.put("lineList", list);
		try {
			entityEvaCtestSumDao.insertEntityEvaCtestSum(insertMap);
			modelMap.put("status", "success");

		} catch (DuplicateKeyException e){
			modelMap.put("status", "failed");
			modelMap.put("msg", "用水分类~单元名称已存在，请重新输入");
			return modelMap;

		}
		return modelMap;
	}

	/**
	 * 刪除數據
	 * 
	 * @param id delete row id
	 * @return 1 成功；0 失败
	 */
	public int deleteEntityEvaCtestSum(List<Integer> listId) {
		try {
			// 使用增强for循环进行删除
			for (int id : listId) {
				entityEvaCtestSumDao.deleteEntityEvaCtestSum(id);
			}
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

	}

	/**
	 * 根据label_id删除数据
	 * 
	 * @param label_id 组号
	 * @return 1 成功；0 失败
	 */
	public int deleteEntityEvaCtestSumByLabelId(int label_id) {
		try {
			entityEvaCtestSumDao.deleteEntityEvaCtestSumByLabelId(label_id);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 根據label_id展示評估數據
	 * 
	 * @param label_id
	 * @return database message
	 */
	public List<Map<String, Object>> selectEntityEvaCtestSum(int label_id) {
		return entityEvaCtestSumDao.selectEntityEvaCtestSum(label_id);
	}

	/**
	 * 更新記錄
	 * 
	 * @param id         記錄id
	 * @param list       記錄數據
	 * @param listColumn 記錄列名
	 * @return 成功返回1，失败返回0
	 */
	public Map<String, Object> updateEntityEvaCtestSum(int id, List<Object> list, List<String> listColumn) {

		Map<String, Object> modelMap = new HashMap<>();

		int length = listColumn.size();
		// 可變字段在固定字段中間
		String strSum = "water_class=" + "'" + list.remove(0) + "'," + "use_id=" + "'" + list.remove(0) + "',"
				+ "water_unit_name=" + "'" + list.remove(0) + "',";

		for (int i = 0; i < length; i++) {
			strSum += listColumn.get(i) + "='" + list.remove(0) + "',";
		}
		strSum = strSum.substring(0, strSum.length() - 1);
		Map<String, Object> sqlMap = new HashMap<>();
		sqlMap.put("id", id);
		sqlMap.put("sql", strSum);
		try {

			entityEvaCtestSumDao.updateEntityEvaCtestSum(sqlMap);
			modelMap.put("status", "success");
		}catch (DuplicateKeyException e){
	        	if(e.getMessage().contains("CLASS_NAME")) {
	        		modelMap.put("status", "failed");
	        		modelMap.put("msg", "用水分类~单元名称已存在，请重新输入");
	        		return modelMap;
	        	}
			}

		return modelMap;
	}

}
